package com.rtos.model.terminal;

import com.rtos.model.disk.Disk;
import com.rtos.model.fileSystem.FileUtil;
import com.rtos.vo.file.diskPane.DiskViewer;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class Terminal {
    //命令行的文本区域
    public TextArea ta;
    //当前起始插入符号位置
    private Integer currentStartCaretPosition;

    public StringBuilder command;
    public DiskViewer diskViewer;
    public Terminal(final TextArea ta, FileUtil fileUtil, TreeView FileTree,DiskViewer diskViewer) {
        this.diskViewer=diskViewer;
        this.ta = ta;
        this.show(ta,">>");
        this.currentStartCaretPosition = this.getCurrentStartCaretPosition(ta);
        this.command = new StringBuilder();
        //判断光标位置是否正确，如果不正确则不能进行输入
        ta.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent event) {
                boolean editable = true;
                if (Terminal.this.currentStartCaretPosition > Terminal.this.getCurrentStartCaretPosition(ta)) {
                    editable = false;
                } else if (Terminal.this.currentStartCaretPosition == Terminal.this.getCurrentStartCaretPosition(ta) && event.getCode() == KeyCode.BACK_SPACE) {
                    editable = false;
                }

                if (!editable) {
                    ta.setEditable(false);
                    Platform.runLater(new Runnable() {
                        public void run() {
                            ta.setEditable(true);
                        }
                    });
                }

            }
        });
        //释放键时发生此事件
        ta.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if(Terminal.this.currentStartCaretPosition <= Terminal.this.getCurrentStartCaretPosition(ta) && event.getCode() ==KeyCode.ENTER) {
                    //当前输入的命令
                    String currentCommand;
                    currentCommand = ta.getText(Terminal.this.currentStartCaretPosition, ta.getText().length() - 1);
                    System.out.println(currentCommand);
                    Terminal.this.command.append(currentCommand);
                    TerminalKernel terminalKernel = TerminalKernel.getInstance();
                    String result = terminalKernel.process(Terminal.this.command.toString(),fileUtil,FileTree,diskViewer);
                    Terminal.this.resetCommand(Terminal.this.command);
                    Terminal.this.show(ta, result);
                    Terminal.this.show(ta, "\n");
                    Terminal.this.show(ta, "\n");
                    Terminal.this.show(ta, ">>");
                    Terminal.this.currentStartCaretPosition = Terminal.this.getCurrentStartCaretPosition(ta);

                }
            }
        });
    }
    public void show(TextArea ta, String message) {
        ta.appendText(message);
    }
    //获得当前文本区域的位置
    public int getCurrentStartCaretPosition(TextArea ta) {
        return ta.getCaretPosition();
    }

    public StringBuilder resetCommand(StringBuilder command) {
        return command.delete(0, command.length());
    }
}
